package sdk8.date;

import java.rmi.ServerException;
import java.text.SimpleDateFormat;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.util.Date;

public class DevUse1 {

    /**
     * 默认日期格式
     */
    private static final String DATE_DEFAULT = "yyyy-MM-dd";

    /**
     * 完整日期格式
     */
    private static final String DATE_DEFAULT_FULL = "yyyy-MM-dd HH:mm:ss";


    public static void main(String[] args) throws ServerException {
        String timeRange = "2022-11-24,2022-11-24";
        LocalDate localDate = strToLocalDateTime(timeRange.split(",")[1]);
        System.out.println(localDate.plusDays(1).atStartOfDay());

        ZoneId zone = ZoneId.systemDefault();
        Instant instant = localDate.plusDays(1).atStartOfDay().atZone(zone).toInstant();
        System.out.println(dateToDefaultFull(Date.from(instant)));

        String term = "222天";
        System.out.println(term.split("天")[0]);

        LocalDateTime startTime = LocalDateTime.of(localDate, LocalTime.MIN);
        LocalDateTime endTime = LocalDateTime.of(localDate, LocalTime.MAX);
        System.out.println(dateToDefaultFull(Date.from(startTime.atZone(ZoneId.systemDefault()).toInstant())));
        System.out.println(dateToDefaultFull(Date.from(endTime.atZone(ZoneId.systemDefault()).toInstant())));
    }

    /**
     * String转LocalDateTime,日期格式目前仅以长度判断,不符合将抛日期不支持异常
     * @author firr
     * @date 2022/7/18 10:55
     * @param str 日期字符串
     * @return java.time.LocalDateTime
     */
    public static LocalDate strToLocalDateTime(String str) throws ServerException {
        DateTimeFormatter df;
        if (str.length() == 10) {
            df = DateTimeFormatter.ofPattern(DATE_DEFAULT);
            return LocalDate.parse(str, df);
        }
//        else if (str.length() == 19) {
//            df = DateTimeFormatter.ofPattern(DATE_DEFAULT_FULL);
//            return LocalDateTime.parse(str, df);
//        }
//        throw new ServiceException(ErrorCodeEnum.TIME_FORMAT_NOT_SUPPORT);
        return null;
    }


    /**
     * @description
     * date类型时间戳转换成标准日期格式字符串
     * @author junjie
     * @since 2022/8/1 10:28
     */
    public static String dateToDefaultFull(Date date) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = dateFormat.format(date);
        return format;
    }
}
